<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form
          ref="searchForm"
          :model="searchForm"
          inline
          :label-width="70"
          class="search-form"
        >
          <Form-item label="商家名称">
            <Input
              type="text"
              v-model="searchForm.storeName"
              placeholder="请输入商家名称"
              clearable
              style="width: 200px"
            />
          </Form-item>
          <!-- <Form-item label="变动时间">
            <DatePicker
              v-model="selectDate"
              type="datetimerange"
              format="yyyy-MM-dd HH:mm:ss"
              clearable
              @on-change="selectDateRange"
              placeholder="选择起始时间"
              style="width: 200px"
            ></DatePicker>
          </Form-item> -->
          <Form-item label="变动时间">
            <DatePicker
              v-model="selectDate"
              type="datetimerange"
              format="yyyy-MM-dd HH:mm"
              clearable
              @on-change="selectDateRange"
              placeholder="选择起始时间"
              style="width: 200px"
            ></DatePicker>
          </Form-item>
          <Button
            @click="handleSearch"
            class="search-btn"
            type="primary"
            icon="ios-search"
            >搜索</Button
          >
        </Form>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        class="mt_10"
        :data="data"
        ref="table"
      ></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50, 100]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
  </div>
</template>

<script>
import { getFund } from "@/api/fund.js";

export default {
  name: "recycling",
  data() {
    return {
      selectDate: [], // 创建时间
      loading: true, // 表单加载状态
      searchForm: {
        // 请求参数
        pageNumber: 1,
        pageSize: 10,
        order: "desc",
        storeName: "", //商家名称
      },
      columns: [
        {
          title: "商家名称",
          key: "storeName",
          tooltip: true,
        },
        {
          title: "钱包类型",
          key: "owner",
          render: (h, params) => {
            if (params.row.owner == "STORE_PROMOTE") {
              return h("div", [h("span", {}, "商家推广钱包")]);
            } else if (params.row.owner == "STORE_SALE") {
              return h("div", [h("span", {}, "商家销售钱包")]);
            }
          },
        },
        {
          title: "钱包余额",
          key: "owner",
          render: (h, params) => {
            if (params.row.owner == "STORE_PROMOTE") {
              return h("div", [h("span", {}, params.row.promote)]);
            } else if (params.row.owner == "STORE_SALE") {
              return h("div", [h("span", {}, params.row.sale)]);
            }
          },
        },

        {
          title: "金额",
          key: "money",
        },
        {
          title: "变动前金额",
          key: "beforeMoney",
        },
        {
          title: "变动后金额",
          key: "afterMoney",
        },
        {
          title: "变动时间",
          key: "createTime",
        },
        {
          title: "业务类型",
          key: "serviceType",
          render: (h, params) => {
            if (params.row.serviceType == "WALLET_WITHDRAWAL") {
              return h("div", [h("span", {}, "余额提现")]);
            } else if (params.row.serviceType == "WALLET_PAY") {
              return h("div", [h("span", {}, "余额支付")]);
            } else if (params.row.serviceType == "WALLET_REFUND") {
              return h("div", [h("span", {}, "余额退款")]);
            } else if (params.row.serviceType == "WALLET_RECHARGE") {
              return h("div", [h("span", {}, "余额充值")]);
            } else if (params.row.serviceType == "WALLET_COMMISSION") {
              return h("div", [h("span", {}, "佣金提成")]);
            } else {
              return h("div", [h("span", {}, "")]);
            }
          },
        },
        {
          title: "变动明细",
          align: "left",
          key: "detail",
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
    };
  },
  methods: {
    // 起止时间从新赋值
    selectDateRange(v) {
      if (v) {
        this.searchForm.startTime = v[0];
        this.searchForm.endTime = v[1];
      }
    },
    // 分页 改变页码
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getData();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.searchForm.pageNumber = 1;
      this.getData();
    },
    // 搜索
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getData();
    },
    defaultTime() {
      var date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth()+1;
      let dd = date.getDate();
      month = month < 10 ? "0" + month : month;
      dd = dd < 10 ? "0" + dd : dd;
      
      let defaultDate = year + "-" + month + "-" + dd +' '+ "00:00:00.0";
      let defaultDates = year + "-" + month + "-" + dd +' '+ "23:59:59.999";
      this.selectDate = [defaultDate, defaultDates];
      this.searchForm.startTime = this.selectDate[0];
      this.searchForm.endTime = this.selectDate[1];
    },
    //查询会员列表
    getData() {
      getFund(this.searchForm).then((res) => {
        if (res.result.records) {
          this.loading = false;
          this.data = res.result.records;
          this.total = res.result.total;
        }
      });
    },
  },
  mounted() {
      this.defaultTime();
    this.getData();    
  },
};
</script>
<style lang="scss" scoped>
/deep/ .ivu-table-wrapper {
  width: 100%;
}
/deep/ .ivu-card {
  width: 100%;
}
.face {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
</style>
